# coding=utf-8
"""
Author: Mr.夏末
@Function: 函数：字符串替换第n个
# https://www.yuque.com/docs/share/6bd17c09-cdf4-4977-948b-489298fe488f
"""


def sreplace(s, n, old, new):
    """替换第n个"""
    # 先替换前n个
    rs = s.replace(old, new, n)
    # 再把前n-1个替换回去
    rs = rs.replace(new, old, n - 1)
    return rs


s = "willhtufeihzhangxinhroyhwillhtufeihzhangxinhroydwillhtufeihzhangxinh" \
    "royhwillhtufeihzhangxinhroydwillhtufeihzhangxinhroyhwillhroy"
s = sreplace(s, 4, "will", "卡卡")
print(s)